package Control;

import java.util.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.text.SimpleDateFormat;

import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SwingWorker;

import Entity.Workshop_BookWorkshopEntity;
import Entity.Workshop_TrainerEntity;
import Entity.Workshop_WorkshopCategory;
import Entity.Workshop_WorkshopEntity;
import Entity.Workshop_WorkshopStatusCode;
import Form.Workshop_UpdateWorkshop_Form;

public class Workshop_UpdateWorkshop_Controller {
	//Object Attributes
	private Workshop_BookWorkshopEntity checkWorkshop = new Workshop_BookWorkshopEntity();
	private Workshop_WorkshopEntity selectedWorkshop, updatedWorkshop;
	private Workshop_TrainerEntity selectedTrainer = new Workshop_TrainerEntity();
	ArrayList<Workshop_WorkshopCategory> categoryList = new ArrayList<Workshop_WorkshopCategory>();
	ArrayList<Workshop_TrainerEntity> trainerList = new ArrayList<Workshop_TrainerEntity>();
	Workshop_WorkshopStatusCode[] statusList = {new Workshop_WorkshopStatusCode("AVA", "Available"), new Workshop_WorkshopStatusCode("CNL", "Cancelled")};
	
	private Workshop_UpdateWorkshop_Form parser;

	public Workshop_UpdateWorkshop_Form getParser() {
		return parser;
	}

	public void setParser(Workshop_UpdateWorkshop_Form parser) {
		this.parser = parser;
	}
	
	public Workshop_UpdateWorkshop_Controller(final Workshop_UpdateWorkshop_Form parser){
		this.setParser(parser);
		selectedWorkshop = parser.getParser().getControl().getSelectedWorkshop();
		
		SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>(){

			@Override
			protected Void doInBackground() throws Exception {
				selectedWorkshop.retrieveAWorkshop();
				categoryList = Workshop_WorkshopCategory.retrieveAllCategory();
				trainerList = Workshop_TrainerEntity.retrieveAllTrainers();
				
				return null;
			}
			
			protected void done(){
				for(int i = 0; i < categoryList.size(); i++){
					getParser().getCategory().addItem(categoryList.get(i).getDescription());
				}
				
				for(int i = 0; i < trainerList.size(); i++){
					getParser().getTrainerNameCombo().addItem(trainerList.get(i).getTrainerName());
				}
				
				getParser().getStatus().addItem(statusList[0].getDescription());
				getParser().getStatus().addItem(statusList[1].getDescription());
				
				generateSelectedWorkshop();

				CommonControls.togglePanelModality(parser);
				parser.getParser().getParser().closeProgressBar();
			}
		};
		CommonControls.togglePanelModality(parser);
		parser.getParser().getParser().getProgressBar();
		worker.execute();
	}
	
	public void generateSelectedWorkshop(){
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
		
		getParser().getLblTitle().setText(selectedWorkshop.getWorkshopName());
		getParser().getLblWorkshopid().setText(selectedWorkshop.getWorkshopId());
		getParser().getWorkshopDate().setText(sdf.format(selectedWorkshop.getWorkshopDateTime()));
		getParser().getCategory().setSelectedItem(selectedWorkshop.getCategory().getDescription());
		getParser().getDescription().setText(selectedWorkshop.getDescription());
		getParser().getVenue().setText(selectedWorkshop.getVenue());
		getParser().getStartDateTime().setModel(new SpinnerDateModel(selectedWorkshop.getStartDateTime(), CommonControls.minusDayToDate(selectedWorkshop.getStartDateTime(), 1), CommonControls.addYearToDate(CommonControls.getCurrentDate(), 1), Calendar.DAY_OF_YEAR));
		getParser().getStartDateTime().setEditor(new JSpinner.DateEditor(getParser().getStartDateTime(), "dd/MM/yyyy HH:mm"));
		getParser().getEndDateTime().setModel(new SpinnerDateModel(selectedWorkshop.getEndDateTime(), CommonControls.minusDayToDate(selectedWorkshop.getEndDateTime(), 1), CommonControls.addYearToDate(CommonControls.getCurrentDate(), 1), Calendar.DAY_OF_YEAR));
		getParser().getEndDateTime().setEditor(new JSpinner.DateEditor(getParser().getEndDateTime(), "dd/MM/yyyy HH:mm"));
		getParser().getCapacity().setValue(selectedWorkshop.getCapacity());
		getParser().getTrainerNameCombo().setSelectedItem(selectedWorkshop.getTrainer().getTrainerName());
		getParser().getTrainerName().setText(selectedWorkshop.getTrainer().getTrainerName());
		getParser().getContactNo().setText(Integer.toString(selectedWorkshop.getTrainer().getContactNo()));
		getParser().getStatus().setSelectedItem(selectedWorkshop.getStatusCode().getDescription());
		
	}
	
	public void setTrainerDetails(){
		if(getParser().getNewTrainer().isSelected()){
			getParser().getTrainerNameCombo().setEnabled(false);
			getParser().getTrainerName().setText("");
			getParser().getTrainerName().setEditable(true);
			getParser().getContactNo().setText("");
			getParser().getContactNo().setEditable(true);
		}
		else{
			getParser().getTrainerNameCombo().setEnabled(true);
			selectedTrainer = trainerList.get(getParser().getTrainerNameCombo().getSelectedIndex());
			getParser().getTrainerName().setEditable(false);
			getParser().getTrainerName().setText(selectedTrainer.getTrainerName());
			getParser().getContactNo().setEditable(false);
			getParser().getContactNo().setText(Integer.toString(selectedTrainer.getContactNo()));
		}
	}
	
	public void updateWorkshop(){
		if(CommonControls.validatePanelFields(parser)){
			updatedWorkshop = selectedWorkshop;
			updatedWorkshop.setCategory(categoryList.get(getParser().getCategory().getSelectedIndex()));
			updatedWorkshop.setDescription(getParser().getDescription().getText());
			updatedWorkshop.setVenue(getParser().getVenue().getText());
			updatedWorkshop.setStartDateTime((Date)getParser().getStartDateTime().getValue());
			updatedWorkshop.setEndDateTime((Date)getParser().getEndDateTime().getValue());
			if(getParser().getNewTrainer().isSelected()){
				Workshop_TrainerEntity newTrainer = new Workshop_TrainerEntity();
				newTrainer.setTrainerId(CommonControls.generateDefaultType5DigitCode('T'));
				newTrainer.setTrainerName(getParser().getTrainerName().getText());
				newTrainer.setContactNo(Integer.parseInt(getParser().getContactNo().getText()));
				newTrainer.createTrainer();
				updatedWorkshop.setTrainer(newTrainer);
			}
			updatedWorkshop.setStatusCode(statusList[(getParser().getStatus().getSelectedIndex())]);
			updatedWorkshop.updateWorkshop();
			JOptionPane.showMessageDialog(null, "Workshop successfully updated");
			getParser().getParser().getControl().getViewSingle().dispose();
			getParser().getParser().getControl().generateList();
			
			checkWorkshop.setWorkshop(updatedWorkshop);
			if(checkWorkshop.getWorkshop().getStatusCode().getStatusCode().equals("CNL")){
				checkWorkshop.deleteBookingForCNL();
			}
		}
	}
}
